package com.atguigu.gulimall.thirdpart.controller;


import com.atguigu.common.utils.BaseResult;
import com.atguigu.gulimall.thirdpart.common.constant.MinioProperties;
import com.atguigu.gulimall.thirdpart.common.utils.MinioUtils;
import com.atguigu.gulimall.thirdpart.config.MinioConfig;
import io.minio.ObjectWriteResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/oss")
public class OSSController {

    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private MinioConfig minioConfig;

    @Autowired
    private MinioProperties minioProperties;

    /**
     * 文件上传
     *
     * @param file
     */
    @PostMapping("/upload")
    public BaseResult upload(@RequestParam("file") MultipartFile file) {
        try {
            //文件名
            String fileName = file.getOriginalFilename();

            String newFileName = System.currentTimeMillis() + "." + StringUtils.substringAfterLast(fileName, ".");
            //类型
            String contentType = file.getContentType();

            LocalDate localDate = LocalDate.now();

            newFileName = localDate.toString() + "/" + newFileName;

            ObjectWriteResponse response = minioUtils.uploadFile(minioProperties.getBucketName(), file, newFileName, contentType);

            // 根据 桶名称 和 文件名称 获取文件外链
            String url = minioUtils.getPresignedObjectUrl(minioProperties.getBucketName(), newFileName);


            Map<String,Object> dataResult = new HashMap<>();

            String[] url_array = url.split("\\?");

            String real_url =  url_array[0] ;

            dataResult.put("url",real_url);

            dataResult.put("fileName",newFileName);

            return BaseResult.ok().put("data",dataResult);
        } catch (Exception e) {
            log.error("上传失败: " + e.getMessage());
            return BaseResult.error("文件上传失败");
        }
    }

    /**
     * 删除
     *
     * @param fileName
     */
    @DeleteMapping("/")
    public void delete(@RequestParam("fileName") String fileName) {
        minioUtils.removeFile(minioProperties.getBucketName(), fileName);
    }

    /**
     * 获取文件信息
     *
     * @param fileName
     * @return
     */
    @GetMapping("/info")
    public String getFileStatusInfo(@RequestParam("fileName") String fileName) {
        return minioUtils.getFileStatusInfo(minioProperties.getBucketName(), fileName);
    }

    /**
     * 获取文件外链
     *
     * @param fileName
     * @return
     */
    @GetMapping("/url")
    public String getPresignedObjectUrl(@RequestParam("fileName") String fileName) {
        return minioUtils.getPresignedObjectUrl(minioProperties.getBucketName(), fileName);
    }

    /**
     * 文件下载
     *
     * @param fileName
     * @param response
     */
    @GetMapping("/download")
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        try {
            InputStream fileInputStream = minioUtils.getObject(minioProperties.getBucketName(), fileName);
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/force-download");
            response.setCharacterEncoding("UTF-8");
            IOUtils.copy(fileInputStream, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载失败");
        }
    }

}